#include "launchpad.h"

#define WDTDisable()    WDTCTL = WDTPW + WDTHOLD

/**
 * Prototypes
 */
static void ConfigCLK_1MHZ(void);
static void ConfigLED(void);
static void ConfigPB(void);

// Exported Interface
void LAUNCHPAD_Init(void) {
    WDTDisable();
    ConfigCLK_1MHZ();
    ConfigLED();
    ConfigPB();
    __enable_interrupt();                      // __bis_status_register(GIE);
    return;
}

/** 
 * Rudeimentary Delay function. 
 */
void delay(unsigned int d) {
    int i;
    for (i = 0; i < d; i++) {
        nop();
    }
}

/**
 * Intrinsic Delay function
 */
void delay_ms(unsigned int ms) { while(ms--) __delay_cycles(1000); }

/**
 * Configure the board
 */
static void ConfigCLK_1MHZ(void) {
    BCSCTL1 = CALBC1_1MHZ;
    DCOCTL  = CALDCO_1MHZ;
}

static void ConfigLED(void) {
    BITSET(P1DIR, LED_G | LED_R);              // LEDs are output
    BITCLR(P1OUT, LED_G + LED_R);              // LEDs off
}

static void ConfigPB(void) {
    BITCLR(P1DIR, USR_BUTTON);                 // SW input
    BITSET(P1IE,  USR_BUTTON);                 // P1.3 interrupt enabled
    BITSET(P1IES, USR_BUTTON);                 // P1.3 Hi/lo edge
    BITCLR(P1IFG, USR_BUTTON);                 // P1.3 IFG cleared
}

